<?php

namespace addons\oil\model;

use think\Model;
use addons\oil\exception\Exception;
use think\Db;

/**
 * 订单操作日志
 */
class OrderAction extends Model
{

    // 表名,不含前缀
    protected $name = 'oil_order_action';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'event_text',
    ];

    protected $hidden = ['deletetime'];

    public static function operAdd($order = null, $oper = null, $type = 'user', $remark = '',$event='')
    {
        //处理操作日志文案-20240527
        if($event=='pay'){//支付成功
//            $remark= "您的油票号:".substr($order['order_sn'], -8)."已支付成功，本次消费立减￥".$order['discount_fee']."。您加油后，请您到订单页确认已加油!会赠送您一次立减机会，请注意查收!";
            $remark= "您的油票号:".substr($order['order_sn'], -8)."已支付成功";
        }elseif ($event=='confirm'){
//            $remark="您申请油票号:".substr($order['order_sn'], -8)."确认已加油，会赠送您一次立减机会请注意查收。";
            $remark="您申请油票号:".substr($order['order_sn'], -8)."确认已加油。";
        }elseif($event=='refund'){
            $remark="您的油票号:".substr($order['order_sn'], -8)." 退款申请已提交，请耐心等待审核。审核通过后，5个工作日内完成退款!";
        }elseif($event=='refund_finish'){
            $remark="您申请油票号:".substr($order['order_sn'], -8)." 款项已原路返回到原支付方，请您注意查收!退款金额:¥".$order['pay_fee']."。";
        }

        $oper_id = empty($oper) ? 0 : (is_array($oper) ? $oper['id'] : $oper->id);
        $self = new self();
        $self->order_id = $order['id'];
        $self->user_id = $order['user_id'];//为了处理订单消息列表加上次字段
        $self->event = $event;
        $self->oper_type = $type;
        $self->oper_id = $oper_id;
        $self->order_status = is_null($order) ? 0 : $order['status'];
        $self->goods_invoice_status = $order['goods_invoice_status']??0;
        $self->service_invoice_status = $order['service_invoice_status']??0;
        $self->aftersale_status = $order['aftersale_status']??0;
        $self->refund_status = $order['refund_status']??0;
        $self->remark = $remark;
        $self->save();

        return $self;
    }

    public function getEventList()
    {
        return [
            'cancel' => '取消订单',
            'pay' => '支付成功',
            'confirm' => '确认订单',
            'refund' => '申请退款',
            'refund_ing' => '退款处理中',
            'refund_refuse' => '退款已拒绝',
            'refund_finish' => '退款完成',
            'hexiao' => '核销完成',
        ];
    }

    public function getEventTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['event']) ? $data['event'] : '');
        $list = $this->getEventList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
